﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json;
using System.IO;

namespace ZMQtoSMSService
{
    public class BsonConvert
    {
        public string BsomSerializerBySingleData(SMSModel sMSModel)
        {
            var memoryStream = new MemoryStream();
            BsonWriter bsonWriter = new BsonWriter(memoryStream);

            JsonSerializer serializer = new JsonSerializer();
            serializer.Serialize(bsonWriter, sMSModel);
            var res = string.Format(BitConverter.ToString(memoryStream.ToArray()));
            return res;
        }


        //bson 反序列化
        public static SMSModel BsonDeserializeBySingleData<T>(string BsonString)
        {
            var memoryStream = new MemoryStream(StringToBytes(BsonString));
            BsonReader reader = new BsonReader(memoryStream);
            JsonSerializer serializer = new JsonSerializer();
            var sMSModel = serializer.Deserialize<SMSModel>(reader);
            return sMSModel;

        }

        private static byte[] StringToBytes(string str)
        {
            String[] arr = str.Split('-');
            byte[] array = new byte[arr.Length];
            for (int i = 0; i < arr.Length; i++) array[i] = Convert.ToByte(arr[i], 16);
            return array;
        }
    }
}
